package gdut.stream.optional;

import gdut.stream.middle.Student;

import java.util.Optional;

public class OptionalTest {
    public static void main(String[] args) {
        Optional<Student> student = GetStudentUtils.getStudent();
//        安全消费
        student.ifPresent(student1 -> System.out.println("student = " + student1));
//        Student student1 = student.get();//这种获取方法不推荐，如果student为null会报java.util.NoSuchElementException: No value present
//        optional也可以像stream流一样过滤
        student.filter(stu -> stu.getAge() > 10).ifPresent(stu -> System.out.println(stu));
//        optional也可以有map操作
        Optional<String> nameStr = student.map(stu -> stu.getName());
        nameStr.ifPresent(str -> System.out.println(str));
    }
}
